<template>
  <div id='UserChat'>
    <div class="container">
      <!-- 固定导航 -->
      <van-nav-bar fixed left-arrow @click-left="$router.back()" title="小思同学"></van-nav-bar>

      <!-- 聊天主体区域 -->
      <div class="chat-list">
        <div v-for="item in list" :key="item">
          <!-- 左侧是机器人小思 -->
          <div class="chat-item left" v-if="item.name === 'xs'">
            <van-image fit="cover" round src="https://img.yzcdn.cn/vant/cat.jpeg" />
            <div class="chat-pao">{{ item.msg }}</div>
          </div>

          <!-- 右侧是当前用户 -->
          <div class="chat-item right" v-else>
            <div class="chat-pao">{{ item.msg }}</div>
            <van-image fit="cover" round src="https://img.yzcdn.cn/vant/cat.jpeg" />
          </div>
        </div>
      </div>

      <!-- 对话区域 -->
      <div class="reply-container van-hairline--top">
        <van-field v-model.trim="word" placeholder="说点什么...">
          <template #button>
            <span @click="send()" style="font-size:12px;color:#999" :class="{haveVal:word}">发送</span>
          </template>
        </van-field>
      </div>
    </div>
  </div>
</template>

<script  setup>
import { Field as vanField, Icon as vanIcon, Image as vanImage, NavBar as vanNavBar, Toast } from 'vant'
import { onBeforeUnmount, reactive, ref, watch, nextTick } from 'vue'
import { io } from 'socket.io-client'
// socket客户端连接
let socket = io('ws://www.liulongbin.top:9999')
// 组件销毁前关闭连接
onBeforeUnmount(() => {
  socket.close()
  socket = null
})
// 建立连接的事件
// socket.on('connect', () => console.log('connect: websocket 连接成功！'))
// 关闭连接的事件
// socket.on('disconnect', () => console.log('disconnect: websocket 连接关闭！'))
// 接收到消息的事件
socket.on('message', msg => {
  list.push({
    name: 'xs',
    msg: msg
  })
})

const word = ref('')
const list = reactive([
  // 1. 只根据 name 属性，即可判断出这个消息应该渲染到左侧还是右侧
])
// 发送消息
const send = () => {
  if (!word.value) return
  socket.emit('send', word.value)
  list.push({ name: 'me', msg: word.value })
  word.value = ''
}

// 自动滚动到页面底部
const scrollToBottom = () => {
  // 1. 获取到所有的聊天 Item 项
  const chatItem = document.querySelectorAll('.chat-item')
  // 2. 获取到最后一项对应的 DOM 元素
  const lastItem = chatItem[chatItem.length - 1]
  if (!lastItem) return
  // 3. 滚动元素的父容器，使被调用 scrollIntoView() 的元素对用户可见
  lastItem.scrollIntoView({
    behavior: 'smooth',
    // 定义垂直方向的对齐（end 表示元素的底端将和其所在滚动区的可视区域的底端对齐）
    block: 'end'
  })
}
watch(() => list.length, () => {
  nextTick(scrollToBottom)
})
</script>

<style lang='less'>
.haveVal {
  color: #4777ee!important;
  font-weight: 700;
}

.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;

  .chat-list {
    height: 100%;
    overflow-y: scroll;

    .chat-item {
      padding: 10px;

      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }

      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;

        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}

.chat-item.right {
  text-align: right;

  .chat-pao {
    margin-left: 0;
    margin-right: 15px;

    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}

.chat-item.left {
  text-align: left;

  .chat-pao {
    margin-left: 15px;
    margin-right: 0;

    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}

.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
